package com.me.study.javaCore.enumm;

import java.util.Map;

import static org.junit.Assert.*;

/**
 * 枚举测试类
 *
 * @author ME
 * @date   2020/1/14
 */
public class TestEnum {

    public static void main(String[] args) {
        // ColorEnum.RED
        ColorEnum red = ColorEnum.valueOf("RED");
        assertEquals("枚举常量的名称：(String) \"RED\"",
                "RED", red.name());

        assertEquals("toString() 等同于 枚举常量的名称：(String) \"RED\"",
                "RED", red.toString());

        assertEquals("枚举常量 自定义的编号属性：(String) 91",
                "91", red.getCode());

        assertEquals("根据 code 获取 displayName：(String) 黄色",
                "黄色", ColorEnum.code2Desc("94"));

        assertEquals("枚举常量 声明顺序的下标（从 0 开始）：(int) 0",
                0, red.ordinal());

        assertEquals("根据声明顺序的下标获取枚举值：",
                ColorEnum.RED, ColorEnum.values()[0]);

        ColorEnum[] constants = ColorEnum.class.getEnumConstants();
        assertEquals("ColorEnum.getEnumConstants() 获取枚举类所有枚举类型，等同于 ColorEnum.values()：",
                ColorEnum.RED, constants[0]);

        System.out.println("\nJDK1.7 开始 switch 支持 Enum：");
        extractedBySwitch(ColorEnum.BLACK);

        System.out.println("\n策略枚举：");
        ColorEnum.doSomeByColor(ColorEnum222.WHITE.name(), "苹果");
        ColorEnum.doSomeByColor(ColorEnum222.GREEN.name(), "苹果");

        System.out.println("\n策略枚举（Function 实现）：");
        ColorEnum.doSomeByColor(ColorEnum.RED.name(), "苹果");
        ColorEnum.doSomeByColor(ColorEnum.BLACK.name(), "苹果");
        Map<String, String> map = BaseEnum.getCode2DescMap(ColorEnum.class);
        System.out.println(map);
    }

    /**
     * 通过 switch 语句做值转换
     * @param colorEnum
     */
    private static void extractedBySwitch(ColorEnum colorEnum) {
        switch (colorEnum) {
            case RED:
                System.out.println("switch + Enum: RED");
                break;
            case GREEN:
                System.out.println("switch + Enum: GREEN");
                break;
            case BLACK:
            case YELLOW:
            case WHITE:
                System.out.println("switch + Enum: " + colorEnum.name());
                break;
            default:
                System.out.println("switch + Enum: unknown");
        }
    }
}
